import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

// 商家账户中心（仅商家账户登录使用）
export const merchantAccountRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/merchantAccount/info',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/merchantAccount',
    component: Layout,
    redirect: '/merchantAccount/info',
    name: 'MerchantAccount',
    meta: {
      title: t('router.merchantAccount'),
      icon: 'ant-design:dashboard-filled'
      // alwaysShow: true
    },
    children: [
      {
        path: 'changePassword',
        name: 'ChangePassword',
        component: () => import('@/views/ChangePassword/ChangePassword.vue'),
        meta: {
          title: t('common.changePassword'),
          noCache: true,
          noTagsView: true,
          hidden: true
        }
      },
      {
        path: 'info',
        name: 'MerchantAccountInfo',
        component: () => import('@/views/MerchantAccount/MerchantAccount.vue'),
        meta: {
          title: t('router.merchantAccount'),
          noCache: true,
          affix: true
        }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    redirect: '/order/groupBuy',
    name: 'Order',
    meta: {
      title: t('router.order'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'groupBuy',
        name: 'OrderGroupBuy',
        component: () => import('@/views/OrderManagement/GroupBuyList/GroupBuyList.vue'),
        meta: {
          title: '订单列表',
          permission: ['GET /order/pageGroupOrder']
        }
      }
    ]
  },
  {
    path: '/activity',
    component: Layout,
    redirect: '/activity/normal',
    name: 'Activity',
    meta: {
      title: t('router.activity'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'normal',
        name: 'ActivityNormal',
        component: () => import('@/views/ActivityManagement/NormalList/NormalList.vue'),
        meta: {
          title: '普通活动',
          permission: ['GET /active/page']
        }
      },
      {
        path: 'rank',
        name: 'ActivityRank',
        component: () => import('@/views/ActivityManagement/Rank/Rank.vue'),
        meta: {
          title: '分销排名',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/activity/normal'
        }
      },
      {
        path: 'verifyRecord',
        name: 'ActivityRank',
        component: () => import('@/views/ActivityManagement/VerifyRecord/VerifyRecord.vue'),
        meta: {
          title: '核销记录',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/activity/normal'
        }
      },
      {
        path: 'groupBuy',
        name: 'ActivityGroupBuy',
        component: () => import('@/views/ActivityManagement/GroupBuyList/GroupBuyList.vue'),
        meta: {
          title: t('router.activityGroupBuy'),
          permission: ['GET /active/page']
        }
      },
      {
        path: 'groupBuyPageGroup',
        name: 'ActivityGroupBuyPageGroup',
        component: () => import('@/views/ActivityManagement/PageGroup/PageGroup.vue'),
        meta: {
          title: '拼团团组',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/activity/groupBuy'
        }
      }
    ]
  },
  {
    path: '/integral',
    component: Layout,
    redirect: '/integral/merchant',
    name: 'Integral',
    meta: {
      title: '积分管理',
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'goodsList',
        name: 'IntegralGoodsList',
        component: () => import('@/views/Integral/Merchant/goodsList.vue'),
        meta: {
          title: '积分商品',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/integral/merchant'
        }
      },
      {
        path: 'detail',
        name: 'IntegralDetail',
        component: () => import('@/views/Integral/Detail/list.vue'),
        meta: {
          title: '积分明细'
        }
      },
      {
        path: 'order',
        name: 'IntegralOrder',
        component: () => import('@/views/Integral/Order/list.vue'),
        meta: {
          title: '积分订单'
        }
      }
    ]
  },
  {
    path: '/report',
    component: Layout,
    redirect: '/report/commission',
    name: 'Report',
    meta: {
      title: '数据报表',
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'commission',
        name: 'Commission',
        component: () => import('@/views/Report/Commission/list.vue'),
        meta: {
          title: '佣金查询'
        }
      }
    ]
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Dashboard',
    meta: {
      title: t('router.dashboard'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'analysis',
        component: () => import('@/views/Dashboard/Analysis.vue'),
        name: 'Analysis',
        meta: {
          title: t('router.analysis'),
          noCache: true,
          affix: true
        }
      }
    ]
  },
  {
    path: '/changePassword',
    component: Layout,
    redirect: '/changePassword/edit',
    name: 'ChangePassword',
    meta: {
      hidden: true,
      canTo: true
    },
    children: [
      {
        path: 'edit',
        component: () => import('@/views/ChangePassword/ChangePassword.vue'),
        name: 'ChangePasswordEdit',
        meta: {
          title: t('common.changePassword'),
          noCache: true,
          noTagsView: true
        }
      }
    ]
  },
  {
    path: '/merchantManagement',
    name: 'MerchantManagement',
    component: Layout,
    redirect: '/merchantManagement/list',
    meta: {
      title: t('router.merchantManagement'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'invitation',
        name: 'MerchantInvitation',
        component: () => import('@/views/MerchantManagement/MerchantInvitation.vue'),
        meta: {
          title: t('router.merchantInvitationManagement'),
          permission: ['GET /inviteBusinessUrl/page']
        }
      },
      {
        path: 'review',
        name: 'MerchantSettlementReview',
        component: () =>
          import(
            '@/views/MerchantManagement/MerchantSettlementReview/MerchantSettlementReview.vue'
          ),
        meta: {
          title: t('router.merchantSettlementReview'),
          permission: ['GET /businessEnterApply/page']
        }
      },
      {
        path: 'list',
        name: 'MerchantList',
        component: () => import('@/views/MerchantManagement/MerchantList/MerchantList.vue'),
        meta: {
          title: t('router.merchantList'),
          permission: ['GET /business/page']
        }
      },
      {
        path: 'detail',
        component: () => import('@/views/MerchantManagement/MerchantDetail/MerchantDetail.vue'),
        name: 'MerchantDetail',
        meta: {
          permission: ['GET /merchant/pageRelationBusiness'],
          title: '关联商户',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/merchantManagement/list'
        }
      },
      {
        path: 'config',
        name: 'MerchantConfiguration',
        component: () =>
          import('@/views/MerchantManagement/MerchantConfiguration/MerchantConfiguration.vue'),
        meta: {
          title: t('router.merchantConfiguration'),
          permission: ['GET /memberCard/page']
        }
      }
    ]
  },
  {
    path: '/business',
    component: Layout,
    redirect: '/business/list',
    name: 'Business',
    meta: {
      title: t('router.businessManagement'),
      icon: 'ant-design:dashboard-filled'
    },
    children: [
      {
        path: 'enterList',
        name: 'enterList',
        component: () => import('@/views/BusinessManagement/EnterList.vue'),
        meta: {
          title: '商户入驻管理'
        }
      },
      {
        path: 'list',
        name: 'businessList',
        component: () => import('@/views/BusinessManagement/BusinessList.vue'),
        meta: {
          title: '商户列表',
          permission: ['GET /merchant/page']
        }
      },
      {
        path: 'detail',
        component: () => import('@/views/BusinessManagement/BusinessDetail/BusinessDetail.vue'),
        name: 'BusinessDetail',
        meta: {
          permission: ['GET /merchant/pageRelationBusiness'],
          title: t('router.BusinessDetail'),
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/business/list'
        }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    redirect: '/order/groupBuy',
    name: 'Order',
    meta: {
      title: t('router.order'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'groupBuy',
        name: 'OrderGroupBuy',
        component: () => import('@/views/OrderManagement/GroupBuyList/GroupBuyList.vue'),
        meta: {
          title: '订单列表',
          permission: ['GET /order/pageGroupOrder']
        }
      }
      // {
      //   path: 'application',
      //   name: 'OrderApplication',
      //   component: () => import('@/views/OrderManagement/ApplicationList/ApplicationList.vue'),
      //   meta: {
      //     title: t('router.orderApplication'),
      //     permission: ['GET /order/pageEnrollOrder']
      //   }
      // }
    ]
  },
  {
    path: '/activity',
    component: Layout,
    redirect: '/activity/normal',
    name: 'Activity',
    meta: {
      title: t('router.activity'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'normal',
        name: 'ActivityNormal',
        component: () => import('@/views/ActivityManagement/NormalList/NormalList.vue'),
        meta: {
          title: '普通活动',
          permission: ['GET /active/page']
        }
      },
      {
        path: 'rank',
        name: 'ActivityRank',
        component: () => import('@/views/ActivityManagement/Rank/Rank.vue'),
        meta: {
          title: '分销排名',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/activity/normal'
        }
      },
      {
        path: 'verifyRecord',
        name: 'ActivityRank',
        component: () => import('@/views/ActivityManagement/VerifyRecord/VerifyRecord.vue'),
        meta: {
          title: '核销记录',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/activity/normal'
        }
      },
      // {
      //   path: 'application',
      //   name: 'ActivityApplication',
      //   component: () => import('@/views/ActivityManagement/ApplicationList/ApplicationList.vue'),
      //   meta: {
      //     title: t('router.activityApplication'),
      //     permission: ['GET /active/page']
      //   }
      // },
      // {
      //   path: 'external',
      //   name: 'ActivityExternal',
      //   component: () => import('@/views/ActivityManagement/ExternalList/ExternalList.vue'),
      //   meta: {
      //     title: t('router.activityExternal'),
      //     permission: ['GET /active/page']
      //   }
      // },
      {
        path: 'groupBuy',
        name: 'ActivityGroupBuy',
        component: () => import('@/views/ActivityManagement/GroupBuyList/GroupBuyList.vue'),
        meta: {
          title: t('router.activityGroupBuy'),
          permission: ['GET /active/page']
        }
      },
      {
        path: 'groupBuyPageGroup',
        name: 'ActivityGroupBuyPageGroup',
        component: () => import('@/views/ActivityManagement/PageGroup/PageGroup.vue'),
        meta: {
          title: '拼团团组',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/activity/groupBuy'
        }
      }
    ]
  },
  {
    path: '/agent',
    component: Layout,
    redirect: '/agent/list',
    name: 'Agent',
    meta: {},
    children: [
      {
        path: 'list',
        name: 'agentList',
        component: () => import('@/views/AgentManagement/AgentList/AgentList.vue'),
        meta: {
          title: t('router.agentList'),
          icon: 'ant-design:dashboard-filled',
          permission: ['GET /agent/page']
        }
      },
      {
        path: 'detail',
        name: 'agentDetail',
        component: () => import('@/views/AgentManagement/AgentDetail/AgentDetail.vue'),
        meta: {
          title: '关联商家列表',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/agent/list'
        }
      }
    ]
  },
  {
    path: '/appUser',
    component: Layout,
    redirect: '/appUser/list',
    name: 'AppUser',
    meta: {},
    children: [
      {
        path: 'list',
        name: 'appUserList',
        component: () => import('@/views/AppUserList/AppUserList.vue'),
        meta: {
          title: t('router.appUserList'),
          icon: 'ant-design:dashboard-filled',
          permission: ['GET /appUser/page']
        }
      }
    ]
  },
  {
    path: '/financial',
    component: Layout,
    redirect: '/financial/agentWithdrawalBill',
    name: 'Financial',
    meta: {
      title: t('router.financial'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'agentWithdrawalBill',
        name: 'FinancialAgentWithdrawalBill',
        component: () =>
          import('@/views/FinancialManagement/agentWithdrawalBill/agentWithdrawalBill.vue'),
        meta: {
          title: t('router.agentWithdrawalBill'),
          permission: ['GET /agentWithdrawalBill/page']
        }
      },
      {
        path: 'merchantAmtWithdrawalBill',
        name: 'FinancialMerchantAmtWithdrawalBill',
        component: () =>
          import(
            '@/views/FinancialManagement/merchantAmtWithdrawalBill/merchantAmtWithdrawalBill.vue'
          ),
        meta: {
          title: t('router.merchantAmtWithdrawalBill'),
          permission: ['GET /merchantAmtWithdrawalBill/page']
        }
      },
      {
        path: 'settlementPoolAmtWithdrawalBill',
        name: 'FinancialSettlementPoolAmtWithdrawalBill',
        component: () =>
          import(
            '@/views/FinancialManagement/settlementPoolAmtWithdrawalBill/settlementPoolAmtWithdrawalBill.vue'
          ),
        meta: {
          title: '结算池商户本金提现管理',
          permission: ['GET /settlementPool/amtPage']
        }
      },
      {
        path: 'settlementPoolSubsidyWithdrawalBill',
        name: 'FinancialSettlementPoolSubsidyWithdrawalBill',
        component: () =>
          import(
            '@/views/FinancialManagement/settlementPoolSubsidyWithdrawalBill/settlementPoolSubsidyWithdrawalBill.vue'
          ),
        meta: {
          title: '结算池商户补贴提现管理',
          permission: ['GET /settlementPool/subsidyPage']
        }
      },
      {
        path: 'check',
        component: () =>
          import('@/views/FinancialManagement/withdrawalBillCheck/withdrawalBillCheck.vue'),
        name: 'WithdrawalBillCheck',
        meta: {
          permission: ['GET /withdrawalBill/check/info'],
          title: '商户本金提现核算',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/financial/settlementPoolAmtWithdrawalBill'
        }
      },
      {
        path: 'check1',
        component: () =>
          import('@/views/FinancialManagement/withdrawalBillCheck/withdrawalBillCheck.vue'),
        name: 'WithdrawalBillCheck1',
        meta: {
          permission: ['GET /withdrawalBill/check/info'],
          title: '商户补贴提现核算',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/financial/settlementPoolSubsidyWithdrawalBill'
        }
      },
      {
        path: 'merchantSubsidyWithdrawalBill',
        name: 'FinancialMerchantSubsidyWithdrawalBill',
        component: () =>
          import(
            '@/views/FinancialManagement/merchantSubsidyWithdrawalBill/merchantSubsidyWithdrawalBill.vue'
          ),
        meta: {
          title: t('router.merchantSubsidyWithdrawalBill'),
          permission: ['GET /merchantSubsidyWithdrawalBill/page']
        }
      },
      {
        path: 'memberCardBill',
        name: 'FinancialMemberCardBill',
        component: () => import('@/views/FinancialManagement/memberCardBill/memberCardBill.vue'),
        meta: {
          title: t('router.memberCardBill'),
          permission: ['GET /memberCardBill/page']
        }
      },
      {
        path: 'memberFeeBill',
        name: 'FinancialMemberFeeBill',
        component: () => import('@/views/FinancialManagement/memberFeeBill/memberFeeBill.vue'),
        meta: {
          title: t('router.memberFeeBill'),
          permission: ['GET /memberFeeBill/page']
        }
      },
      {
        path: 'subsidyAmtBill',
        name: 'FinancialSubsidyAmtBill',
        component: () => import('@/views/FinancialManagement/subsidyAmtBill/subsidyAmtBill.vue'),
        meta: {
          title: t('router.subsidyAmtBill'),
          permission: ['GET /subsidyAmtBill/page']
        }
      },
      {
        path: 'merchantAmtBill',
        name: 'FinancialMerchantAmtBill',
        component: () => import('@/views/FinancialManagement/merchantAmtBill/merchantAmtBill.vue'),
        meta: {
          title: t('router.merchantAmtBill'),
          permission: ['GET /merchantAmtBill/page']
        }
      },
      {
        path: 'commissionBill',
        name: 'FinancialCommissionBill',
        component: () => import('@/views/FinancialManagement/commissionBill/commissionBill.vue'),
        meta: {
          title: t('router.commissionBill'),
          permission: ['GET /commissionBill/page']
        }
      },
      {
        path: 'merchantSubsidyRecharge',
        name: 'FinancialMerchantSubsidyRecharge',
        component: () =>
          import('@/views/FinancialManagement/merchantSubsidyRecharge/merchantSubsidyRecharge.vue'),
        meta: {
          title: '结算池商家补贴充值管理',
          permission: ['GET /merchantSubsidyRecharge/page']
        }
      }
    ]
  },
  {
    path: '/operation',
    component: Layout,
    redirect: '/operation/article',
    name: 'Operation',
    meta: {
      title: '运营管理',
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'tag',
        name: 'OperationTag',
        component: () => import('@/views/Operation/Tag/Tag.vue'),
        meta: {
          title: '标签管理'
        }
      }
    ]
  },
  {
    path: '/integral',
    component: Layout,
    redirect: '/integral/merchant',
    name: 'Integral',
    meta: {
      title: '积分管理',
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'merchant',
        name: 'IntegralMerchant',
        component: () => import('@/views/Integral/Merchant/list.vue'),
        meta: {
          title: '积分商家'
        }
      },
      {
        path: 'goodsList',
        name: 'IntegralGoodsList',
        component: () => import('@/views/Integral/Merchant/goodsList.vue'),
        meta: {
          title: '积分商品',
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/integral/merchant'
        }
      },
      {
        path: 'detail',
        name: 'IntegralDetail',
        component: () => import('@/views/Integral/Detail/list.vue'),
        meta: {
          title: '积分明细'
        }
      },
      {
        path: 'order',
        name: 'IntegralOrder',
        component: () => import('@/views/Integral/Order/list.vue'),
        meta: {
          title: '积分订单'
        }
      }
    ]
  },
  {
    path: '/college',
    component: Layout,
    redirect: '/college/article',
    name: 'College',
    meta: {
      title: t('router.college'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'category',
        name: 'CollegeCategory',
        component: () => import('@/views/College/Category/Category.vue'),
        meta: {
          title: t('router.articleCategory'),
          permission: ['GET /contentCategory/page']
        }
      },
      {
        path: 'article',
        name: 'CollegeArticle',
        component: () => import('@/views/College/Article/Article.vue'),
        meta: {
          title: t('router.article'),
          permission: ['GET /content/page']
        }
      }
    ]
  },
  {
    path: '/report',
    component: Layout,
    redirect: '/report/commission',
    name: 'Report',
    meta: {
      title: '数据报表',
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'commission',
        name: 'Commission',
        component: () => import('@/views/Report/Commission/list.vue'),
        meta: {
          title: '佣金查询'
        }
      }
    ]
  },
  {
    path: '/feedback',
    component: Layout,
    redirect: '/feedback/list',
    name: 'Feedback',
    meta: {
      title: t('router.feedback'),
      icon: 'ep:question-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/Feedback/Feedback.vue'),
        name: 'FeedbackList',
        meta: {
          title: t('router.feedback'),
          permission: ['GET /feedback/page']
        }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: 'System',
    meta: {
      title: t('router.systemManager'),
      icon: 'eos-icons:role-binding',
      alwaysShow: true
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/System/User/User.vue'),
        name: 'SystemUser',
        meta: {
          title: t('router.user'),
          permission: ['GET /admin/user/list/more']
        }
      },
      {
        path: 'role',
        component: () => import('@/views/System/Role/Role.vue'),
        name: 'SystemRole',
        meta: {
          title: t('router.role'),
          permission: ['POST /role/list/more']
        }
      },
      {
        path: 'withdrawalConfig',
        component: () => import('@/views/System/WithdrawalConfig/WithdrawalConfig.vue'),
        name: 'WithdrawalConfig',
        meta: {
          title: t('router.withdrawalConfig'),
          permission: ['GET /withdrawalConfig/details']
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
